package inc.zhugastrov.ui;

import inc.zhugastrov.sellerform.MainSellerForm;
import inc.zhugastrov.users.Account;
import inc.zhugastrov.users.Position;

import java.awt.*;
import java.awt.event.*;
import static inc.zhugastrov.dbactions.DBActions.addUser;
import static inc.zhugastrov.dbactions.DBActions.checkUser;
import javax.swing.*;

import static inc.zhugastrov.dbactions.DBActions.initDB;

@SuppressWarnings("serial")
public class AuthorizationForm extends JFrame {
	// Variables declaration
	private JLabel usernameLabel;
	private JLabel passwordLabel;
	private JTextField textField;
	private JPasswordField passwordField1;
	private JButton loginButton;
	private JButton exitButton;
	private JPanel contentPane;
	private static Account authorizedUser;

	// End of variables declaration

	public static Account getAuthorizedUser() {
		return authorizedUser;
	}

	public AuthorizationForm() {
		super();
		create();
		this.setVisible(true);
	}

	private void create() {
		// Thread thread = new Thread(new Runnable() {
		//
		// @Override
		// public void run() {
		initDB();
		
		// adding seller account for testing
		Account sellerAcc = new Account();
		sellerAcc.setId(1L);
		sellerAcc.setName("Seller");
		sellerAcc.setLogin("seller");
		sellerAcc.setPosition(Position.SELLER);
		sellerAcc.setPassword("qwerty");
		addUser(sellerAcc);

		// adding manager account for testing
		Account managerAcc = new Account();
		managerAcc.setId(2L);
		managerAcc.setName("Manager");
		managerAcc.setLogin("manager");
		managerAcc.setPosition(Position.MANAGER);
		managerAcc.setPassword("qwerty");
		addUser(managerAcc);

		// }
		// });
		// thread.start();

		usernameLabel = new JLabel();
		passwordLabel = new JLabel();
		textField = new JTextField();
		passwordField1 = new JPasswordField();
		loginButton = new JButton();
		contentPane = (JPanel) this.getContentPane();
		exitButton = new JButton("Exit");
		//
		// jLabel1
		//
		usernameLabel.setHorizontalAlignment(SwingConstants.LEFT);
		// jLabel1.setForeground(new Color(0, 0, 255));
		usernameLabel.setText("Username:");
		//
		// jLabel2
		//
		passwordLabel.setHorizontalAlignment(SwingConstants.LEFT);
		// jLabel2.setForeground(new Color(0, 0, 255));
		passwordLabel.setText("Password:");
		//
		// jTextField1
		//
		// jTextField1.setForeground(new Color(0, 0, 255));
		// jTextField1.setSelectedTextColor(new Color(0, 0, 255));
		textField.setToolTipText("Enter your username");
		//
		// exitButton
		//
		exitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});

		//
		// jPasswordField1
		//
		// jPasswordField1.setForeground(new Color(0, 0, 255));
		passwordField1.setToolTipText("Enter your password");
		//
		// jButton1
		//
		// jButton1.setBackground(new Color(204, 204, 204));
		// jButton1.setForeground(new Color(0, 0, 255));
		loginButton.setText("Login");
		this.getRootPane().setDefaultButton(loginButton);
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jButton1_actionPerformed(e);
			}

		});
		//
		// contentPane
		//
		
		contentPane.setLayout(null);
		contentPane.setBorder(BorderFactory.createEtchedBorder());
		// contentPane.setBackground(new Color(204, 204, 204));
		addComponent(contentPane, usernameLabel, 5, 10, 106, 18);
		addComponent(contentPane, passwordLabel, 5, 47, 97, 18);
		addComponent(contentPane, textField, 110, 10, 183, 22);
		addComponent(contentPane, passwordField1, 110, 45, 183, 22);
		addComponent(contentPane, loginButton, 120, 75, 83, 28);
		addComponent(contentPane, exitButton, 220, 75, 83, 28);
		//
		// login
		//
		this.setTitle("Login To Members Area");
		this.setLocation(new Point(76, 182));
		this.setSize(new Dimension(335, 141));
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setResizable(false);
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container, Component c, int x, int y,
			int width, int height) {
		c.setBounds(x, y, width, height);
		container.add(c);
	}

	private void jButton1_actionPerformed(ActionEvent e) {
		String role = new String();
		String username = new String(textField.getText());
		String password = new String(passwordField1.getPassword());

		if (username.equals("") || password.equals("")) // If password and
														// username is empty >
														// Do this >>>
		{
			loginButton.setEnabled(false);
			JLabel errorFields = new JLabel(
					"<HTML><FONT COLOR = Black>You must enter a username and password to login.</FONT></HTML>");
			JOptionPane.showMessageDialog(null, errorFields);
			textField.setText("");
			passwordField1.setText("");
			loginButton.setEnabled(true);
			this.setVisible(true);
		} else if ((authorizedUser = checkUser(username, password))!= null) {
			role = authorizedUser.getPosition();
			JOptionPane.showMessageDialog(this, "Success", "Info",
					JOptionPane.INFORMATION_MESSAGE);
			if (role.equalsIgnoreCase("seller"))
				new MainSellerForm();
			else if (role.equalsIgnoreCase("manager"))
				new UserManagment();
			this.dispose();

		}

		else {
			JOptionPane.showMessageDialog(this,
					"Mistaaaaaake!!!! Wrooong login or password. Try again if you want", "Error",
					JOptionPane.ERROR_MESSAGE);
			textField.setText("");
			passwordField1.setText("");
			return;
		}

	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception ex) {
			System.out.println("Failed loading L&F: ");
			System.out.println(ex);
		}
		new AuthorizationForm();
	};

}
